<?php

defined('BASEPATH') or die('forbidden');

class multiexcelseal_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
        header("Content-type:text/html;charset=UTF-8"); 
        date_default_timezone_set('PRC'); 
    }
    function index() 
    {   
        $this->view->display("seal/multiexcelseal/index");
    }
    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $playerpath=MULTIUPLOADPATH.'excel/sealplayer/';
        $accountpath=MULTIUPLOADPATH.'excel/sealaccount/';
        $seal_file['player']=get_filenamesbydir($playerpath,2);
        $seal_file['account']=get_filenamesbydir($accountpath,2);
        for($i=0;$i<count($seal_file['player']);$i++)
        {
            $sealarray[$i]['type']      ='批量封角色';
            $sealarray[$i]['filename']  ='sealplayer/'.$seal_file['player'][$i]['filename'];
            $sealarray[$i]['time']      =$seal_file['player'][$i]['time'];
        }
        for($j=0;$j<count($seal_file['account']);$j++)
        {
            $sealarray[$i]['type']      ="<font color=\"blue\">批量封帐号</font>";
            $sealarray[$i]['filename']  ='sealaccount/'.$seal_file['account'][$j]['filename'];
            $sealarray[$i]['time']      =$seal_file['account'][$j]['time'];
            $i++;
        }
        unset($seal_file);
        $total=count($sealarray);
        for ($k=$offset;$k<$total;$k++) {         
            $n['type']      =$sealarray[$k]['type'];
            $n['intime']    =date('Y-m-d H:i:s',$sealarray[$k]['time']);
            $n['filename']  =$sealarray[$k]['filename'];
            $rows[] = array('id' => str_replace('/', '*', $n['filename']), 'cell' => ($n));
        }
        
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }
    function add() {
        if(is_submit())
        {            
            if($_FILES['file']['error']>0)
            {
                echo "Error: " . $_FILES["file"]["error"] . "<br />";
            }
            else
            {
                $filetype   =$_FILES['file']['type'];
                
                if($filetype=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                {
                    $tmp_name   =$_FILES['file']['tmp_name'];
                    $file_name  =$_FILES['file']['name'];
                    $type=$_POST['type'];
                    if($type=='multi_player_seal')
                    {
                        $excelpath=MULTIUPLOADPATH.'excel/sealplayer/';      
                    }
                    else if($type=='multi_account_seal')
                    {
                        $excelpath=MULTIUPLOADPATH.'excel/sealaccount/';     
                    }
                    if(file_exists($excelpath.$file_name))
                    {
                        unlink($excelpath.$file_name);
                    }
                    $uploadresult=move_uploaded_file($tmp_name, $excelpath.$file_name);
                    if($uploadresult)
                    {
                        $newfilename=$excelpath.'seal'.date('YmdHis',time()).'.xlsx';     
                        //$_SESSION['multiexcelseal']=$newfilename;
                        $renameresult=rename($excelpath.$file_name, $newfilename);
                        //$message='upload file success';
                        //echo "<script>alert('".$message."');</script>";
                        
                        $area_host  =  get_area_list_url();
                        $excelarray =  readExcel($newfilename);    
                        for($i=2;$i<=count($excelarray);$i++)
                        {
                            if($type=='multi_player_seal')
                            {   //echo $excelarray[$i]['A'].'*'.$excelarray[$i]['B']."<br>";
                                $area_url                                       =$area_host[$excelarray[$i]['A']];
                                $sealarray[$area_url][0]['area_url']      =$area_url;                
                                $sealarray[$area_url][$i]['name']         =$excelarray[$i]['B'];
                                $sealarray[$area_url][$i]['seal_time']    =$excelarray[$i]['C']*86400;
                            }
                            else if($type=='multi_account_seal')
                            {
                                $sealarray[$area_url][$i]['name']         =$excelarray[$i]['A'];
                                $sealarray[$area_url][$i]['seal_time']    =$excelarray[$i]['B']*86400;
                            }
                        }
                        unset($excelarray);
                        unset($area_host);
                        $sealarray=  array_values($sealarray);
                        $area_host=  get_area_list_host();

                        for($j=0;$j<count($sealarray);$j++)
                        {
                            unset($jsonarray);
                            if($type=='multi_player_seal')
                            {
                                $area_url=$sealarray[$j][0]['area_url'];
                                unset($sealarray[$j][0]);
                                $jsonarray=array_values($sealarray[$j]);
                                $json=json_encode($jsonarray);
                                $url=$area_url.'json_str='.$json.'&action=43';
                                $strResult[$area_url]=  vpost($url);
                                if ($strResult[$area_url] === FALSE)
                                    $errorStr .= $area_host[$area_url] . translate('C_TIPS_ERROR_TEXT') . "\r\n";
                                //var_dump($strResult[$area_url]);                              
                            }
                            else if($type=='multi_account_seal')
                            {
                                $jsonarray=array_values($sealarray[$j]);
                                $json=json_encode($jsonarray);
                                $url=$this->config['host_url'].'json_str='.$json.'&action=44';
                                $strResult=  vpost($url);
                                if ($strResult === FALSE)
                                    $errorStr = $area_host[$this->config['host_url']] . translate('C_TIPS_ERROR_TEXT') . "\r\n";
                            }                
                        }
                        unset($sealarray);
                        //echo $url."<br>";
                        if (empty($errorStr))
                            $result = "seal success";
                        else
                            $result=$errorStr;
                        echo "<script>alert('".$result."');</script>";           
                    }
                    else {
                        $message='fail upload file';
                        echo "<script>alert('".$message."');</script>";
                    }
                }
                else
                {
                    $message='please upload Excel2007 file!';
                    echo "<script>alert('".$message."');</script>";
                }               
            }
        }        
        $this->view->display("seal/multiexcelseal/add");
    }
    
    function edit($id)
    {
        $excelpath  =  MULTIUPLOADPATH.'excel/'.str_replace('*', '/', $id);      
        $excelarray =  readExcel($excelpath);
        for($i=1;$i<=count($excelarray);$i++)
        {
            if($i==1)
            {
                $string="<tr bgcolor='#cccfff'>
                            <th width='60px'>{$excelarray[$i]['A']}</th>
                            <th>{$excelarray[$i]['B']}</th>
                            <th>{$excelarray[$i]['C']}</th>
                         </tr>";
            }
            else 
           {
               $string.="<tr bgcolor='#ffffff'>
                            <td>{$excelarray[$i]['A']}</td>
                            <td>{$excelarray[$i]['B']}</td>
                            <td>{$excelarray[$i]['C']}</td>
                         </tr>";
           }
        }
        $this->view->assign('string',$string);
        $this->view->display("seal/multiexcelseal/edit");
    }
}